-
-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add public API for injection & registration #105
Conversation
@@ -188,6 +188,18 @@ export default TestModule.extend({ | |||
} | |||
hook.apply(module, Array.prototype.slice.call(arguments, 1)); | |||
}; | |||
context.register = function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, these belong in the main test-module
file, as they are not really related to components...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Will do.
This looks nice! I've been reaching into the container and using register for a while in tests but it will be great to have a blessed path to doing so 👍 |
In an app, we encourages users to use injection over container.lookup, and we encourage `application.register` over `registry.register`. But in our integration tests, there is no good alternative to direct `this.container.lookup` and `this.registry.register`. So I added `register` and `inject` capability directly to the integration test context. `register` is fairly trivial: test('registering something', function(assert) { this.register('component:x-foo', Ember.Component.extend({ classNames: ['i-am-x-foo'] })); this.render(hbs`{{x-foo}}`); assert.equal(this.$('.i-am-x-foo').length, 1, 'found x-foo'); }); `inject` lets you inject anything into the test context: test('injecting a service', function(assert) { this.inject.service('notifications'); this.render(hbs`{{my-notification-viewer}}`); // The notifications service has been injected into our test context, // so we can access it here and tell it to do something. this.get('notifications').receivedNotification("Hello world"); assert.equal(this.$('.notification').text(), 'Hello world'); }) You can also give the injected thing an alternate name using `as`: test('injecting a service with aliased name', function(assert) { this.inject.service('notifications', { as: 'messages' }); this.render(hbs`{{my-notification-viewer}}`); // The notifications service has been injected into our test context // as this.get('messages'). this.get('messages').receivedNotification("Hello world"); assert.equal(this.$('.notification').text(), 'Hello world'); })
I moved the methods into the base test-context so they're available in all test types, and updated some tests to show that they work. |
Add public API for injection & registration
In an app, we encourages users to use injection over container.lookup,
and we encourage
application.register
overregistry.register
. But in our integration tests, there is no good alternative to directthis.container.lookup
andthis.registry.register
.So I added
register
andinject
capability directly to the integration test context.register
is fairly trivial:inject
lets you inject anything into the test context:You can also give the injected thing an alternate name using
as
: